%% parameters
Q = zeros(6,8);% The value to reflect the strength of every stage. It's necessary to assign the value of Q
Q = [0,1,1.3,1.7,2.2,2.9,3.7,4.8;0.1,1.4,1.7,2.1,2.6,3.3,4.1,5.2;0.2,1.8,2.1,2.5,3.0,3.7,4.5,5.6;0.3,2.2,2.5,2.9,3.4,4.0,4.9,6.0;0.4,2.6,2.9,3.3,3.8,4.4,5.3,6.4;0.5,3.0,3.3,3.7,4.2,4.8,5.7,6.8];
Rc = zeros(6,7); % The reward to pull and get the character
Rw = zeros(5,8); % The reward to pull and get the weapon
c = -1; % Constellation
w = 0; % Refinement Rank
n = 0; % Time of pulling for a stage
Vc = zeros(6,8,91,81); % The expectation of the reward(including the long-term reward) while pulling the character
Vw = zeros(6,8,91,81); % The expectation of the reward(including the long-term reward) while pulling the weapon
V = zeros(6,8,91,81); % The expectation of the reward(including the long-term reward) while choosing the better banner

pread = fileread('pdf_dice_data.json');
pc = jsondecode(pread).character1;
pw = jsondecode(pread).weapon;
for tt=78:81
    pw(tt) = 1;
end
Vc(6,8)=0;
Vw(6,8)=0;
V(6,8)=0;

have = 4;
w1 = 0;
c1 = -1;
n1 = 0;
m1 = 0;
have1 = max(have+n1,have+m1+10);
if(have1>90)
    have1=90;
end
l = 1; % You can change it into a smaller number
lambda = l*have1/100;

%% Calculate rewards
% Calculate Rc
for c = -1:(6-1)
    for w = 0:5
        Rc(w+1,c+2) = Q(w+1,c+3)-Q(w+1,c+2);
    end
end

% Calculate Rw
for c = -1:6
    for w = 0:(5-1)
        Rw(w+1,c+2) = Q(w+2,c+2)-Q(w+1,c+2);
    end
end



%% Calculate V(6,:) and V(:,8) using Bellman Equation
for c = -1:(6-1)
Vc(6,c+2,91,:) = Rc(6,c+2);
V(6,c+2,91,:) = Vc(6,c+2,91,:);
for n=89:-1:0
    Vc(6,c+2,n+1,:) = (Rc(6,c+2)+lambda*Vc(6,c+3,n+2,:))*pc(n+1) + lambda*Vc(6,c+2,n+2,:)*(1-pc(n+1));
    V(6,c+2,n+1,:) = Vc(6,c+2,n+1,:);
end
end

for w = 0:(5-1)
Vw(w+1,8,:,81) = Rw(w+1,8);
V(w+1,8,:,81) = Vw(w+1,8,:,81);
for m=79:-1:0
    Vw(w+1,8,:,m+1) = (Rw(w+1,8)+lambda*Vw(w+2,8,:,m+2))*pw(m+1) + lambda*Vw(w+1,8,:,m+2)*(1-pw(m+1));
    V(w+1,8,:,m+1) = Vw(w+1,8,:,m+1);
end
end



%% Calculate other V using Bellman Equation
for t = 5:-1:1
for c = -1:(6-1)
Vc(t,c+2,91,:) = Rc(t,c+2);
V(t,c+2,91,:) = Vc(t,c+2,91);
for m=0:80
for n=89:-1:0
    Vc(t,c+2,n+1,m+1) = (Rc(t,c+2)+lambda*V(t,c+3,n+2,m+1))*pc(n+1) + lambda*Vc(t,c+2,n+2,m+1)*(1-pc(n+1));
end
end

Vc(t,c+2,:,81) = Rc(t,c+2);
V(t,c+2,:,81) = Vc(t,c+2,:,81);
for n=0:90
for m=79:-1:0
    Vw(t,c+2,n+1,m+1) = (Rw(t,c+2)+lambda*V(t+1,c+2,n+1,m+2))*pw(m+1) + lambda*Vw(t,c+2,n+1,m+2)*(1-pw(m+1));
end
end

for n=0:90
for m=0:80
    V(t,c+2,n+1,m+1) = max(Vc(t,c+2,n+1,m+1),Vw(t,c+2,n+1,m+1));
end
end
end
end



if(Vw(w1+1,c1+2,n1+2,m1+1)>Vc(w1+1,c1+2,n1+1,m1+2))
    display("weapon");
else
    display("character");
end